package qqserver.service;

import java.net.Socket;
import java.util.*;

/**
 * @author DCSGO
 * @version 1.0
 * 该类用于管理连接的主机的 Socket
 */
public class ManageServerConnectClientThreads {
    private static final HashMap<String, ServerConnectClientThread> hm = new HashMap<>();

    public static ArrayList<Socket> getAllClientSockets() {
        ArrayList<Socket> res = new ArrayList<>();
        Collection<ServerConnectClientThread> values = hm.values();
        for (ServerConnectClientThread serverConnectClientThread : values) {
            res.add(serverConnectClientThread.getClientSocket());
        }
        return res;
    }

    public static ArrayList<Socket> getOtherClientSockets(String exceptId) {
        if (hm.size() <= 1) {
            return null;
        }
        ArrayList<Socket> res = new ArrayList<>();
        Set<String> Ids = hm.keySet();
        for (String id : Ids) {
            if (!id.equals(exceptId)) {
                res.add(hm.get(id).getClientSocket());
            }
        }
        return res;
    }

    public static void remove(String id) {
        hm.remove(id);
    }

    public static void add(String id, ServerConnectClientThread thread) {
        hm.put(id, thread);
    }

    public static ServerConnectClientThread get(String id) {
        return hm.get(id);
    }

    public static String getAllUsers() {
        StringBuilder res = new StringBuilder();
        Set<String> keys = hm.keySet();

        for (String user : keys) {
            res.append(user).append(" ");
        }
        return new String(res);
    }
}
